#ifndef filter_h
#define filter_h
int **gaussian_filter_pgm(int **img_array, int height, int width, int w_size, float sigma)
{
	int **img_array1, **img_array2,row, col, k_size,wi,wj;
	float **kernel,temp;
         
	img_array1=g_zeropadding2D(img_array, height, width, w_size);
                    
	img_array2=alloc2Dint(height, width);
	kernel=Gaussian_Kernel(sigma,w_size);

	k_size=(int)floor((float)w_size/2);
	for(row=k_size;row<height;row++)
	{
                for(col=k_size;col<width;col++)
                {
                            temp=0.0;
                            for(wi=-k_size;wi<k_size+1;wi++)
                            {
                                    for(wj=-k_size;wj<k_size+1;wj++)
                                    {
                                            temp=temp+(img_array1[row+wi][col+wj]*kernel[wi+k_size][wj+k_size]);
                                    }
                            }
                           img_array2[row-k_size][col-k_size]=(int)temp;
                }
	}
	return img_array2;

}
int ***gaussian_filter_ppm(int ***img_array, int height, int width, int depth, int w_size, float sigma)
{
	int  ***img_array2,row, col, k_size,wi,wj, k;
	float **kernel,temp;
	//img_array1=g_zeropadding2D(img_array, height, width, w_size);
	
	img_array2=alloc3Dint(height, width, depth);
	
	kernel=Gaussian_Kernel(sigma,w_size);
	
	k_size=(int)floor((float)w_size/2);
	for(row=k_size;row<(height-k_size);row++)
	{
                for(col=k_size;col<(width-k_size);col++)
                {
                         for(k=0;k<depth;k++)
                         {
                            temp=0.0;

                            for(wi=-k_size;wi<k_size+1;wi++)
                            {
                                    for(wj=-k_size;wj<k_size+1;wj++)
                                    {
                                           // printf("%d\t%d\t%d\n",row+wi, col+wj,k);
                                            temp=temp+(img_array[row+wi][col+wj][k]*kernel[wi+k_size][wj+k_size]);
                                    }
                            }
                           img_array2[row-k_size][col-k_size][k]=(int)temp;
                         }
                }
	}
	return img_array2;

}
int **convertgrayimg(int ***img, int n_row, int n_col)
{

    int row, col, **temp_img=NULL;
    temp_img=alloc2Dint(n_row, n_col);


    for(row=s_ind_row;row<e_ind_row;row++)
    {
        for(col=s_ind_col;col<e_ind_col;col++)
        {
            if(img[row][col][0]==255 && img[row][col][1]==255 && img[row][col][2]==255)
            {
                 temp_img[row][col]=255;
            }
        }


    }
   return temp_img;
}
int **median_filter(int ***img, int n_row, int n_col, int w_size)
{

    int row, col, **temp_img=NULL, **temp_img2=NULL, *temparray=NULL, w1, w2, count, ind1;
    temp_img=alloc2Dint(n_row, n_col);
    temp_img2=alloc2Dint(n_row, n_col);
    temparray=alloc1Dint(w_size*w_size);
    for(row=s_ind_row;row<e_ind_row;row++)
    {
        for(col=s_ind_col;col<e_ind_col;col++)
        {
            if(img[row][col][0]==255 && img[row][col][1]==255 && img[row][col][2]==255)
            {
                 temp_img[row][col]=255;
            }
        }


    }
     ind1=(int)floor(w_size/2);
      for(row=s_ind_row;row<e_ind_row;row++)
      {
           for(col=s_ind_col;col<e_ind_col;col++)
            {
                count=0;

                 for(w1=-ind1;w1<ind1+1;w1++)
                 {
                      for(w2=-ind1;w2<ind1+1;w2++)
                         {
                             temparray[count]=temp_img[row+w1][col+w2];
                             count++;
                         }
                 }
                 temparray=sort1Dint(temparray, count);
                 temp_img2[row][col]=temparray[(int)(floor(count/2))];


            }
      }
   for(row=0;row<n_row;row++)
   {
       free(temp_img[row]);
   }
   free(temp_img);
    free(temparray);
 return temp_img2;

}
#else
#endif
